/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vue;

import Controleur.IncidentFormControleur;
import Controleur.TypeFormulaire;
import static Controleur.TypeFormulaire.AFFICHAGE;
import static Controleur.TypeFormulaire.CREATION;
import static Controleur.TypeFormulaire.EDITION;
import Modele.Incident;
import Modele.Technicien;
import Utils.Constantes;
import nemovelo.NemoVelo;

/**
 *
 * @author 20cent
 */
public class IncidentForm extends javax.swing.JDialog {

    private TypeFormulaire mode;
    private IncidentFormControleur controleur;
    private Incident incident;

    /**
     * Creates new form IncidentForm
     */
    public IncidentForm(java.awt.Frame parent, boolean modal, TypeFormulaire mode) {
        super(parent, modal);
        initComponents();

        //initialisation du controleur
        controleur = new IncidentFormControleur(this);

        this.mode = mode;
        // on active les controles en fonction du mode
        appliquerMode();

        //on génère les comboBox
        cb_technicien.removeAllItems();
        cb_statut.removeAllItems();
        cb_statut.addItem(Constantes.STATUT_OUVERT);
        cb_statut.addItem(Constantes.STATUT_EN_COURS);
        cb_statut.addItem(Constantes.STATUT_TRAITE);
        cb_statut.addItem(Constantes.STATUT_CLOS);
        cb_priorite.removeAllItems();
        cb_priorite.addItem(Constantes.PRIORITE_BASSE);
        cb_priorite.addItem(Constantes.PRIORITE_MOYENNE);
        cb_priorite.addItem(Constantes.PRIORITE_HAUTE);
cb_technicien.addItem(null);
            for (Technicien technicien : NemoVelo.techniciens) {
                cb_technicien.addItem(technicien);
            }
    }

    public IncidentForm(java.awt.Frame parent, boolean modal, TypeFormulaire mode, Incident incident) {
        this(parent, modal, mode);
        this.incident = incident;
        if (mode != TypeFormulaire.CREATION) {
            tf_id.setText(String.valueOf(incident.getMId()));
            tf_noserie.setText(incident.getmVelo().toString());
            tf_dateouv.setText(incident.getMDateOuverture().toString());
            if (incident.getMDateFermeture() != null) {
                tf_dateferm.setText(incident.getMDateFermeture().toString());
            }
            tf_typeinc.setText(incident.getmTypInc().toString());
            cb_statut.setSelectedItem(incident.getMStatut());
            cb_priorite.setSelectedItem(incident.getIncidentProrite());
            ta_commentaire.setText(incident.getIncidentCommentaire());
            cb_technicien.setSelectedItem(incident.getmTechnicien());
            
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        label_titre = new javax.swing.JLabel();
        label_id = new javax.swing.JLabel();
        tf_id = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        tf_noserie = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tf_typeinc = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        cb_technicien = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        tf_dateouv = new javax.swing.JTextField();
        tf_dateferm = new javax.swing.JTextField();
        cb_statut = new javax.swing.JComboBox();
        cb_priorite = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        ta_commentaire = new javax.swing.JTextArea();
        panel_boutons = new javax.swing.JPanel();
        btn_annuler = new javax.swing.JButton();
        btn_activ_modif = new javax.swing.JButton();
        btn_update = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        label_titre.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        label_titre.setText("Incident");

        label_id.setText("ID Incident");

        tf_id.setEditable(false);

        jLabel2.setText("Vélo numéro de série");

        tf_noserie.setEditable(false);

        jLabel3.setText("Type d'incident");

        tf_typeinc.setEditable(false);

        jLabel4.setText("Technicien");

        cb_technicien.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel5.setText("Date ouverture");

        jLabel6.setText("Date Fermeture");

        jLabel7.setText("Statut");

        jLabel8.setText("Priorite");

        jLabel9.setText("Commentaire");

        tf_dateouv.setEditable(false);

        tf_dateferm.setEditable(false);

        cb_statut.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        cb_priorite.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        ta_commentaire.setColumns(20);
        ta_commentaire.setRows(5);
        jScrollPane1.setViewportView(ta_commentaire);

        btn_annuler.setText("Annuler");
        btn_annuler.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_annulerActionPerformed(evt);
            }
        });
        panel_boutons.add(btn_annuler);

        btn_activ_modif.setText("Modifier");
        btn_activ_modif.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_activ_modifActionPerformed(evt);
            }
        });
        panel_boutons.add(btn_activ_modif);

        btn_update.setText("Enregistrer");
        btn_update.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_updateActionPerformed(evt);
            }
        });
        panel_boutons.add(btn_update);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel2)
                                            .addComponent(label_id)
                                            .addComponent(jLabel3)
                                            .addComponent(jLabel4)
                                            .addComponent(jLabel5)
                                            .addComponent(jLabel6)
                                            .addComponent(jLabel7)
                                            .addComponent(jLabel8)
                                            .addComponent(jLabel9))
                                        .addGap(28, 28, 28)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(tf_id)
                                            .addComponent(tf_noserie)
                                            .addComponent(tf_typeinc)
                                            .addComponent(cb_technicien, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(tf_dateouv)
                                            .addComponent(tf_dateferm)
                                            .addComponent(cb_statut, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(cb_priorite, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(37, 37, 37)
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(138, 138, 138)
                                .addComponent(label_titre)))
                        .addGap(0, 28, Short.MAX_VALUE))
                    .addComponent(panel_boutons, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 355, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label_titre)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label_id)
                    .addComponent(tf_id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tf_noserie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tf_typeinc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(cb_technicien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(tf_dateouv, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(tf_dateferm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(cb_statut, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(cb_priorite, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel9)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(panel_boutons, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_annulerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_annulerActionPerformed
        this.dispose();
    }//GEN-LAST:event_btn_annulerActionPerformed

    private void btn_activ_modifActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_activ_modifActionPerformed
        mode = TypeFormulaire.EDITION;
        appliquerMode();
    }//GEN-LAST:event_btn_activ_modifActionPerformed

    private void btn_updateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_updateActionPerformed

        //création de la chaine d'outils
        incident.setMStatut(cb_statut.getSelectedItem().toString());
        incident.setIncidentProrite(cb_priorite.getSelectedItem().toString());
        incident.setmTechnicien((Technicien) cb_technicien.getSelectedItem());
        incident.setIncidentCommentaire(ta_commentaire.getText());
        controleur.modifier(incident);

    }//GEN-LAST:event_btn_updateActionPerformed

    private void appliquerMode() {
        switch (mode) {
            case AFFICHAGE:
                activerChamps(false);
                //activation des boutons
                btn_activ_modif.setVisible(true);
                btn_annuler.setVisible(false);
                btn_update.setVisible(false);
                break;
            case CREATION:
                activerChamps(true);
                //activation des boutons
                btn_activ_modif.setVisible(false);
                btn_annuler.setVisible(true);
                btn_update.setVisible(false);
                break;
            case EDITION:
                activerChamps(true);
                //activation des boutons
                btn_activ_modif.setVisible(false);
                btn_annuler.setVisible(true);
                btn_update.setVisible(true);
                break;
            default:
                break;
        }
    }

    private void activerChamps(boolean bool) {
        cb_priorite.setEnabled(bool);
        cb_statut.setEnabled(bool);
        cb_technicien.setEnabled(bool);
        ta_commentaire.setEditable(bool);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(IncidentForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(IncidentForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(IncidentForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(IncidentForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                IncidentForm dialog = new IncidentForm(new javax.swing.JFrame(), true, AFFICHAGE);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_activ_modif;
    private javax.swing.JButton btn_annuler;
    private javax.swing.JButton btn_update;
    private javax.swing.JComboBox cb_priorite;
    private javax.swing.JComboBox cb_statut;
    private javax.swing.JComboBox cb_technicien;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel label_id;
    private javax.swing.JLabel label_titre;
    private javax.swing.JPanel panel_boutons;
    private javax.swing.JTextArea ta_commentaire;
    private javax.swing.JTextField tf_dateferm;
    private javax.swing.JTextField tf_dateouv;
    private javax.swing.JTextField tf_id;
    private javax.swing.JTextField tf_noserie;
    private javax.swing.JTextField tf_typeinc;
    // End of variables declaration//GEN-END:variables
}
